/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef RESET_REG_ACCESS_H
#define RESET_REG_ACCESS_H

/*!
 * @file reset_reg_access.h
 * @brief This file declares or defines reset register access functions
 */

/*******Includes***************************************************************/
#include "device_registers.h"

/*******Definitions************************************************************/
/* Filter Configuration Register */
#define RESET_FCR_FLTEN(x)  (((uint32_t)(((uint32_t)(x)) << RESET_FCR_FLTEN_Pos)) & RESET_FCR_FLTEN_Msk)
#define RESET_FCR_FLTVAL(x) (((uint32_t)(((uint32_t)(x)) << RESET_FCR_FLTVAL_Pos)) & RESET_FCR_FLTVAL_Msk)

/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Set reset function enable or disable for certain reset sources
 *
 * @param[in] regBase: The RESET register base address
 * @param[in] resetControl: Reset control sources
 * @param[in] enable: true: Enable these reset control sources
 *                    false: Disable these reset control sources
 * @return None
 */
static inline void RESET_REG_SetEnable(RESET_Type* regBase, uint32_t resetControl, bool enable)
{
    uint32_t regValue = regBase->CR;

    if (enable) {
        regValue |= resetControl;
    } else {
        regValue &= ~resetControl;
    }

    regBase->CR = regValue;
}

/*!
 * @brief Get current reset status flags
 *
 * @param[in] regBase: The RESET register base address
 * @return Current reset status flags
 */
static inline uint32_t RESET_REG_GetStatusFlags(RESET_Type* regBase)
{
    return regBase->SR;
}

/*!
 * @brief Clear reset status flags for certain reset sources
 *
 * @param[in] regBase: The RESET register base address
 * @param[in] resetFlag: Reset flag sources
 * @return None
 */
static inline void RESET_REG_ClearStatusFlags(RESET_Type* regBase, uint32_t resetFlag)
{
    regBase->SR = resetFlag;
}

/*!
 * @brief Enable or Disable filter
 *
 * @param[in] regBase: The RESET register base address
 * @param[in] enable: true: enable, false: disable
 * @return None
 */
static inline void RESET_REG_SetFilterEnable(RESET_Type* regBase, bool enable)
{
    uint32_t regValue = regBase->FCR;

    regValue &= (~RESET_FCR_FLTEN_Msk);
    regValue |= RESET_FCR_FLTEN(enable ? 1U : 0U);
    regBase->FCR = regValue;
}

/*!
 * @brief Configure filter deglitch width
 *
 * @param[in] regBase: The RESET register base address
 * @param[in] filterWidth: Filter deglitch width
 * @return None
 */
static inline void RESET_REG_ConfigFilterWidth(RESET_Type* regBase, uint16_t filterWidth)
{
    uint32_t regValue = regBase->FCR;

    regValue &= (~RESET_FCR_FLTVAL_Msk);
    regValue |= RESET_FCR_FLTVAL(filterWidth);
    regBase->FCR = regValue;
}

#if defined(__cplusplus)
}
#endif

#endif /* RESET_REG_ACCESS_H */

/*******EOF********************************************************************/

